<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :title="'机构'" :editModel="editModel" :api="api" :form="form"
      :formRules="formRules" @onSave="onSave" @onSaved="saveTab" :autoSave="false">
      <dym-form :editModel="editModel" :componentList="componentList" :formValue.sync="form"
        ></dym-form>
        <el-row>
        <!-- <el-col :xs="12" :sm="12" :md="12" :lg="12" >
          <el-form-item label="是否可用" prop="useable" label-width="100px">
            <el-radio v-model="form.useable" label="1" :disabled="editModel === 'V'">是</el-radio>
            <el-radio v-model="form.useable" label="0" :disabled="editModel === 'V'">否</el-radio>
          </el-form-item>
        </el-col> -->
      </el-row>
    </dialog-edit>
  </div>
</template>

<script>
  import BaseDialog from '@/pages/BaseDialog'
  import DialogEdit from '@/components/dialog/DialogEdit'
  import dymForm from '@/components/element/dymForm'
  import { sysOrg } from '@/api/platformApi'
  export default {
    extends: new BaseDialog(),
    components: {
      DialogEdit,
      dymForm
    },
    data () {
      return {
        // 需要显示的每一项定义在这里
        componentList: [
          {
            label: '上级机构',
            prop: 'parentId', // 表单验证传入的值
            labelWidth: '120px',
            tagId: '0',
            componentName: 'cascaderOrg',
            cols: [12, 12, 12, 12],
            placeholder: '请输入',
            value: 'parentId' // 字段显示的值
          },
          {
            label: '机构名称',
            prop: 'name',
            labelWidth: '120px',
            componentName: 'el-input',
            cols: [12, 12, 12, 12],
            placeholder: '请输入',
            value: 'name'
          },
          {
            label: '机构编码',
            prop: 'code',
            labelWidth: '120px',
            componentName: 'el-input',
            cols: [12, 12, 12, 12],
            placeholder: '请输入',
            value: 'code'
          },
          {
            label: '机构类型',
            prop: 'type',
            labelWidth: '120px',
            componentName: 'dict',
            dictTypeCode: 'org_type',
            cols: [12, 12, 12, 12],
            placeholder: '请输入',
            value: 'type'
          },
          // {
          //   label: '机构级别',
          //   prop: 'grade',
          //   labelWidth: '120px',
          //   componentName: 'dict',
          //   dictTypeCode: 'org_class',
          //   cols: [12, 12, 12, 12],
          //   value: 'grade',
          //   placeholder: '请输入'
          // },
          {
            label: '是否可用',
            prop: 'useable',
            labelWidth: '120px',
            componentName: 'el-switch',
            cols: [12, 12, 12, 12],
            value: 'useable',
            placeholder: '请输入'
          },
          {
            label: '主负责人',
            prop: 'primaryUser',
            labelWidth: '120px',
            componentName: 'el-input',
            cols: [12, 12, 12, 12],
            value: 'primaryUser',
            placeholder: '请输入'
          },
           {
            label: '副负责人',
            prop: 'deputyUser',
            labelWidth: '120px',
            componentName: 'el-input',
            cols: [12, 12, 12, 12],
            value: 'deputyUser',
            placeholder: '请输入'
          },
          {
            label: '联系地址',
            prop: 'address',
            labelWidth: '120px',
            componentName: 'el-input',
            cols: [12, 12, 12, 12],
            value: 'address',
            placeholder: '请输入'
          },
          {
            label: '联系电话',
            prop: 'phone',
            labelWidth: '120px',
            componentName: 'el-input',
            cols: [12, 12, 12, 12],
            value: 'phone',
            placeholder: '请输入'
          },
          {
            label: '传真',
            prop: 'fax',
            labelWidth: '120px',
            componentName: 'el-input',
            cols: [12, 12, 12, 12],
            value: 'fax',
            placeholder: '请输入'
          },
          {
            label: '邮箱',
            prop: 'email',
            labelWidth: '120px',
            componentName: 'el-input',
            cols: [12, 12, 12, 12],
            value: 'email',
            placeholder: '请输入'
          },
          {
            label: '备注',
            prop: 'remarks',
            labelWidth: '120px',
            componentName: 'el-input',
            type: 'textarea',
            cols: [24, 24, 24, 24],
            placeholder: '请输入',
            value: 'remarks'
          }
        ],
        // 表单验证
        formRules: {
          // 表单字段名：[规则]
          name: [{ required: true, message: '必填', trigger: 'blur' }],
          code: [{ required: true, message: '必填', trigger: 'blur' }],
          type: [{ required: true, message: '必填', trigger: 'blur' }],
          grade: [{ required: true, message: '必填', trigger: 'blur' }]
        }
      }
    },
    created () {
      if (this.editModel === 'C') {
        this.$set(this.form, 'useable', '1')
      }
    },
    methods: {
     saveTab () {
        if (!this.saveFlag) {
              let $this = this
          if (this.form.id) {
            sysOrg.update(this.form.id, this.form).then(res => {
                if (res.code === 0) {
              this.dialogVisible = false
              this.$emit('handSearch')
              $this.success('修改成功')
              } else if (res.code === -1) {
                $this.error(res.msg)
              }
            })
          } else {
            sysOrg.create(this.form).then(res => {
              if (res.code === 0) {
                 $this.success('新增成功')
                 this.dialogVisible = false
              } else if (res.code === -1) {
                $this.error(res.msg)
              }
            })
          }
        } else {
          this.dialogVisible = false
          this.$emit('handSearch')
        }
      }
    }
  }
</script>
